package programmercarl.数组.C6;

class Solution {
    public static int[][] generateMatrix(int n) {
        int[][] matrix = new int[n][n];
        //从外层到内层
        int topRow=0;
        int bottomRow=n-1;
        int leftCol=0;
        int rightCol=n-1;
        int val=0;
        while(topRow<=bottomRow && leftCol<=rightCol){
            for(int i=leftCol;i<=rightCol;i++){
                matrix[topRow][i]=++val;
            }
            for(int i=topRow+1;i<=bottomRow;i++){
                matrix[i][rightCol]=++val;
            }
            for(int i=rightCol-1;i>=leftCol;i--){
                matrix[bottomRow][i]=++val;
            }
            for(int i=bottomRow-1;i>topRow;i--){
                matrix[i][leftCol]=++val;
            }
            leftCol++;
            rightCol--;
            topRow++;
            bottomRow--;
        }
        return matrix;
    }

    public static void main(String[] args) {
        generateMatrix(4);
    }
}